package com.inlycat.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.inlycat.entity.Store;
import com.inlycat.entity.Thumb;
import com.inlycat.entity.Tramp;
import com.inlycat.entity.Video;
import com.inlycat.mapper.StoreMapper;
import com.inlycat.mapper.ThumbMapper;
import com.inlycat.mapper.TrampMapper;
import com.inlycat.service.VideoService;

/**
 * @author jarvan4dev@163.com
 * @since 2015年8月21日下午6:19:44
 * @category video访问接口
 **/
@Controller
@RequestMapping("/video")
public class VideoController {
	
	@Resource
	private VideoService videoService;
	
	@Resource
	private StoreMapper storeMapper;
	
	@Resource
	private ThumbMapper thumbMapper;
	
	@Resource
	private TrampMapper trampMapper;
	
	@RequestMapping(value="/list",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> list(String pageNum){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<Video> videos = videoService.list(pageNum);
		if(videos!=null&&videos.size()>0){
			resultMap.put("code", 1);
			resultMap.put("message", "success");
			resultMap.put("data", videos);
		}else{
			resultMap.put("code", 0);
			resultMap.put("message", "获取视频列表失败");
		}
		return resultMap;
	}
	
	@RequestMapping(value="/detail",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> detail(Long id,Long userId){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Video video = videoService.selectDetailById(id);
		
		if(video!=null){
			/**
			 * 是否被收藏
			 */
			Store store = new Store(userId,id);
			video.setHasStore(storeMapper.hasStore(store)>0?true:false);
			
			/**
			 * 是否被赞
			 */
			Thumb thumb = new Thumb(userId,id);
			video.setHasThumb(thumbMapper.hasThumb(thumb)>0?true:false);
			
			/**
			 * 是否被踩
			 */
			Tramp tramp = new Tramp(userId,id);
			video.setHasTramp(trampMapper.hasTramp(tramp)>0?true:false);
			resultMap.put("code", 1);
			resultMap.put("message", "success");
			resultMap.put("data", video);
		}else{
			resultMap.put("code", 0);
			resultMap.put("message", "要查询的影片不存在");
		}
		return resultMap;
	}
	
}
